#ifndef SERVERHANDLER_HH
#define SERVERHANDLER_HH

#include "../library/socket.hh"
#include "../library/protocol.hh"
#include "../library/thread.hh"
#include "../library/mutex.hh"

#include <string>

using namespace std;
using namespace NM;
using namespace DLIB_PROTO;

class ServerHandler : Thread, Mutex
{
private: // variables
    string _address;
    int _port;
    int _id;

    int _threadNumber;                              // Mutex protected
    bool _run;                                      // Mutex protected

private: // main thread variables
    Socket _network;
    Protocol _server;

public: // interface
    ServerHandler();
    ServerHandler(string address, int port);

    Socket *Connect();
    int GetId();
    int GetClientNumber();

    void Start();
    void Stop();

    void loop();
    bool IsRunning();

private: // internals
    bool init();
    void setRunning(bool run);
    void setClientNumber(int number);
};

void *server_handler_starter(void *data);

#endif // SERVERHANDLER_HH
